package cn.jyc.ssm03.ctrl;

import cn.jyc.ssm03.pojo.Car;
import cn.jyc.ssm03.pojo.Customer;
import cn.jyc.ssm03.service.CarService;
import cn.jyc.ssm03.service.CustService;
import cn.jyc.ssm03.service.MenuListService;
import cn.jyc.ssm03.service.NewsService;
import org.apache.commons.io.FileUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.util.UUID;

@Controller
public class NewsCtrl {
    @Autowired
    private NewsService ns;
    @Autowired
    private MenuListService mls;
    @Autowired
    private CustService cs;
    @Autowired
    private CarService carse;

    @ResponseBody
    @RequestMapping("/delCar")
    public String delCar(String identity){
        return carse.delCar(identity);
    }

    @ResponseBody
    @RequestMapping("/updCar")
    public String updCar(Car car, HttpServletRequest req) throws IOException {
        System.out.println("接收到的车辆为：" + car);
        if (car.getCarimg().contains("/cache/")) {
            String carimg = car.getCarimg();
            String fname = carimg.substring(carimg.lastIndexOf('/') + 1);
            String realPath1 = req.getServletContext().getRealPath("upload/cache/");
            File srcImg = new File(realPath1, fname);
            String realPath = req.getServletContext().getRealPath("upload/");
            File dstImg = new File(realPath, fname);
            FileUtils.moveFile(srcImg, dstImg);
            car.setCarimg("upload/" + fname);
            System.out.println("新增车辆的图片为：" + car.getCarimg());
            System.out.println("最终新增车辆为：" + car);
        }
        return carse.updCar(car);
    }

    @ResponseBody
    @RequestMapping("/addCar")
    public String addCar(Car car, HttpServletRequest req) throws IOException {
        System.out.println("接收到的车辆为：" + car);
        String carimg = car.getCarimg();
        String fname = carimg.substring(carimg.lastIndexOf('/') + 1);
        String realPath1 = req.getServletContext().getRealPath("upload/cache/");
        File srcImg = new File(realPath1, fname);
        String realPath = req.getServletContext().getRealPath("upload/");
        File dstImg = new File(realPath, fname);
        FileUtils.moveFile(srcImg, dstImg);
        car.setCarimg("upload/" + fname);
//        System.out.println("新增车辆的图片为：" + car.getCarimg());
//        System.out.println("最终新增车辆为：" + car);
        return carse.addCar(car);
    }

    @ResponseBody
    @RequestMapping("/uploadImg")
    public String uploadImg(MultipartFile cimg, HttpServletRequest req) throws IOException {
        if (cimg.isEmpty()) {
            return "error";
        }
        String url = "upload/cache/readme.md";
        String oldName = cimg.getOriginalFilename();
        String name = UUID.randomUUID().toString().replace("-", "");
        name = name + oldName.substring(oldName.lastIndexOf("."));
//        System.out.println("新文件名为：" + name);
        String realPath = req.getServletContext().getRealPath("upload/cache/");
        File file = new File(realPath, name);
        FileUtils.writeByteArrayToFile(file, cimg.getBytes());
        url = "upload/cache/" + name;
//        System.out.println("最终路径名为：" + url);
        return url;
    }

    @ResponseBody
    @RequestMapping("qryCar")
    public String qryCar(@RequestParam(value = "page", defaultValue = "1") Integer p1,
                         @RequestParam(value = "rows", defaultValue = "10") Integer p2, Car car) {
//        System.out.println("查询：" + car);
        return carse.qryCar(p1, p2, car);
    }

    @ResponseBody
    @RequestMapping("updCust")
    public String updCust(Customer cus) {
//        System.out.println("修改：" + cus);
        return cs.updCust(cus);
    }

    @ResponseBody
    @RequestMapping("delCust")
    public String getCust(String identity) {
//        System.out.println(identity);
        return cs.delCust(identity);
    }

    @ResponseBody
    @RequestMapping("getCust")
    public String getCust(@RequestParam(value = "page", defaultValue = "1") Integer p1,
                          @RequestParam(value = "rows", defaultValue = "10") Integer p2,
                          Customer cus) {
//        System.out.println(cus);
        return cs.getCust(p1, p2, cus);
    }

    @ResponseBody
    @RequestMapping("addCust")
    public String addCust(Customer cus) {
        return cs.add(cus);
    }

    @ResponseBody
    @RequestMapping("/getMenus")
    public String getMenus() {
        return mls.getMenu();
    }

    @ResponseBody
    @RequestMapping("/getMenus2")
    public String getMenus2(HttpSession session) {
        Integer uid = (Integer) session.getAttribute("uid");
        return mls.getMenu2(uid);
    }

    @ResponseBody
    @RequestMapping("/getNews")
    public String getNews() {
        String news = ns.getNews();
//        System.out.println("新闻内容：" + news);
        return news;
    }

    @ResponseBody
    @RequestMapping("/getNews2")
    public String getNews2() {
        String news = ns.getNews2();
//        System.out.println("公告内容：" + news);
        return news;
    }

    @RequestMapping("/toP/{url}")
    public String toPage(@PathVariable String url) {
//        System.out.println("======跳转的路径" + url);
        return url;
    }
}
